package net.risesoft.risebi.collection.web.admin;

import java.util.ArrayList;
import java.util.List;

import net.risesoft.risebi.collection.model.ReportPeriodType;
import net.risesoft.risebi.collection.model.Service;
import net.risesoft.risebi.collection.service.ReportPeriodTypeManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.util.GUID;
import net.risesoft.risebi.commons.util.JSONUtil;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;


/**
 * indication action
 * 本类提供报告期类型管理Action
 * @author <a href="mailto:gf7@163.com">关锋</a>
 * @version $Revision$
 * 
 */
@Namespace("/admin/ReportPeriodTypeAction")
public class ReportPeriodTypeAction extends BaseAction{
	
	private String treeId = "reportperiodtype-tree";	
	private ReportPeriodType reportPeriodType;	
	private ReportPeriodTypeManager reportPeriodTypeManager;	
	private boolean halfyear;	
	private boolean qurater;	
	private boolean month;
	
	@Action(value="prepareListType",
			results={@Result(name="success", location="/admin/reportperiod/listreportperiodtype.jsp")}
	)
	public String prepareListType(){
		return SUCCESS;
	}

	@Action(value="prepareListInstance",
			results={@Result(name="success", location="/admin/reportperiod/listreportperiodinstance.jsp")}
	)
	public String prepareListInstance(){
		return SUCCESS;
	}

	@Action(value="prepareListInstance4tree",
			results={@Result(name="success", location="/admin/reportperiod/listreportperiodinstance4tree.jsp")}
	)
	public String prepareListInstance4tree(){
		try{
			ArrayList al = new ArrayList();
			ReportPeriodType root = (ReportPeriodType)reportPeriodTypeManager.getObject(ReportPeriodType.class, this.node);
			reportPeriodTypeManager.recursiveGetAll(root, "parentReportPeriodType", al);
			String s = "";
			for(Object o : al){
				ReportPeriodType rpt = (ReportPeriodType)o;
				if(s.length()==0){
					s = "'" + rpt.getGuid() + "'";
				}else{
					s = s + "," + "'" + rpt.getGuid() + "'";
				}
			}
			
			List lst = reportPeriodTypeManager.find("select rp.reportPeriodType.guid,rp.guid,rp.name from ReportPeriod rp where rp.reportPeriodType.guid in (" + s + ")");
			this.json = JSONUtil.getJsonString(null,null,lst);
		}catch(Exception e){
			this.json = "[]";
		} 
		
		return SUCCESS;
	}

	@Action(value="refreshJson")
	public String refreshJson() throws Exception {
		try{
			List<ReportPeriodType> lst = null;
			String currentServiceGuid = this.getDefaultService().getGuid();
			
			if(this.active_node_id==null || this.active_node_id.trim().length()==0){
				//lst = reportPeriodTypeManager.getObjects(ReportPeriodType.class);
				lst = reportPeriodTypeManager.find("from ReportPeriodType rpt where rpt.service.guid='" + currentServiceGuid + "'");
			}else{
				lst = reportPeriodTypeManager.find("from ReportPeriodType rpt where rpt.parentReportPeriodType.guid='" + this.active_node_id + "' and rpt.service.guid='" + currentServiceGuid + "'");
			}
			
			ArrayList al = new ArrayList();	
			for(ReportPeriodType t : lst){
				boolean isleaf = true;
				int count = reportPeriodTypeManager.getCount("select count(*) from ReportPeriodType t where t.parentReportPeriodType.guid = '" + t.getGuid() + "' and t.service.guid='" + currentServiceGuid + "'");
				if(count>0){
					isleaf = false;
				}
				LazyDynaBean ldb = new LazyDynaBean();
				ldb.set("guid", t.getGuid());
				ldb.set("parentGuid", t.getParentGuid());
				ldb.set("name", t.getName());
				ldb.set("code", t.getCode());
				ldb.set("isleaf", isleaf);
				al.add(ldb);
			}	
			this.json = JSONUtil.getJsonString("include","guid,code,name,isleaf,parentGuid",al);
		}catch(Exception e){
			this.json = "[]";
		} 

		return AJAX;
	}

	@Action(value="prepareInit",
			results={@Result(name="success", location="/admin/reportperiod/initreportperiodtype.jsp")}
	)
	public String prepareInit(){
		return SUCCESS;
	}
	
	@Action(value="init",
			results={@Result(name="success", type="transfer" , params={"method","reportPeriodTypeInit"})}
	)
	public String init() throws Exception {
		//判断是否有常用报告期，有则退出。
		if(reportPeriodTypeManager.isHas())
			throw new Exception("您不能初始化，已有常用报告期被使用！");
		String parentGuid=null;
		reportPeriodType=new ReportPeriodType();
		String guid = new GUID().toString();
		reportPeriodType.setCode("YEAR");
		reportPeriodType.setName("年报");
		reportPeriodType.setGuid(guid);
		reportPeriodTypeManager.saveObject(reportPeriodType);
		parentGuid=guid;
		if (isHalfyear()){
			reportPeriodType=new ReportPeriodType();
			guid = new GUID().toString();
			reportPeriodType.setGuid(guid);
			reportPeriodType.setCode("HALFYEAR");
			reportPeriodType.setName("半年报");
			ReportPeriodType parentReportPeriodType = new ReportPeriodType();
			parentReportPeriodType.setGuid(parentGuid);
			reportPeriodType.setParentReportPeriodType(parentReportPeriodType);
			reportPeriodTypeManager.saveObject(reportPeriodType);
			parentGuid=guid;
		}
		if (isQurater()){
			reportPeriodType=new ReportPeriodType();
			guid = new GUID().toString();
			reportPeriodType.setGuid(guid);
			reportPeriodType.setCode("QURATER");
			reportPeriodType.setName("季报");
			ReportPeriodType parentReportPeriodType = new ReportPeriodType();
			parentReportPeriodType.setGuid(parentGuid);
			reportPeriodType.setParentReportPeriodType(parentReportPeriodType);
			reportPeriodTypeManager.saveObject(reportPeriodType);
			parentGuid=guid;
		}
		if(isMonth()){
			reportPeriodType=new ReportPeriodType();
			guid = new GUID().toString();
			reportPeriodType.setGuid(guid);
			reportPeriodType.setCode("MONTH");
			reportPeriodType.setName("月报");
			ReportPeriodType parentReportPeriodType = new ReportPeriodType();
			parentReportPeriodType.setGuid(parentGuid);
			reportPeriodType.setParentReportPeriodType(parentReportPeriodType);
			reportPeriodTypeManager.saveObject(reportPeriodType);
		}
		
		return SUCCESS;
	}
	
	@Action(value="prepareAdd",
			results={@Result(name="success", location="/admin/reportperiod/addreportperiodtype.jsp")}
	)
	public String prepareAdd() throws Exception {
		return SUCCESS;
	}
	
	@Action(value="prepareEdit",
			results={@Result(name="success", location="/admin/reportperiod/editreportperiodtype.jsp")}
	)
	public String prepareEdit() throws Exception {
		reportPeriodType = (ReportPeriodType)this.reportPeriodTypeManager.getObject(ReportPeriodType.class, node);
		return SUCCESS;
	}
	
	@Action(value="add")
	public String add() throws Exception{
		String guid = new GUID().toString();
		reportPeriodType.setGuid(guid);
		reportPeriodType.setCode(reportPeriodType.getCode().toUpperCase());
		reportPeriodTypeManager.saveObject(reportPeriodType);
		
		return TRANSFER;
	}
	
	@Action(value="edit")
	public String edit(){
		reportPeriodType.setCode(reportPeriodType.getCode().toUpperCase());
		reportPeriodTypeManager.saveObject(reportPeriodType);
		
		node=reportPeriodType.getGuid();
		editedName=reportPeriodType.getName();
		
		return TRANSFER;
	}
	
	@Action(value="prepareAddChildren",
			results={@Result(name="success", location="/admin/reportperiod/addreportperiodtypechildren.jsp")}
	)
	public String prepareAddChildren(){
		
		return SUCCESS;
	}
	
	@Action(value="remove")
	public String remove(){
		reportPeriodTypeManager.removeObject(ReportPeriodType.class, node);
		return TRANSFER;
	}
	
	@Action(value="removeReportPeriodType")
	public String removeReportPeriodType() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			reportPeriodTypeManager.removeObject(ReportPeriodType.class, node);
			
			ldb.set("success", true);
			ldb.set("reason", "删除成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}
		
		return AJAX;		
	}
	
	@Action(value="initialReportPeriodType")
	public String initialReportPeriodType() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			Service service = this.getDefaultService();
			String serviceGuid = service.getGuid();
			
//			判断一下是否有常规的报告期或者已经初始化了
			String hql = "select count(*) from ReportPeriodType r where r.service.guid='"+serviceGuid+"'";
			int count = reportPeriodTypeManager.getCount(hql);
			if(count>0)
				throw new Exception("已经有报告期类型存在了，无需初始化，若需要初始化，则把报告期类型删除后再进行初始化");
			
			String[][] strColl = {
					{new GUID().toString(),"year","年报"},
					{new GUID().toString(),"halfyear","半年报"},
					{new GUID().toString(),"quarter","季报"},
					{new GUID().toString(),"month","月报"},
					{new GUID().toString(),"week","周报"},
					{new GUID().toString(),"day","日报"}
			};
			
			ReportPeriodType parentRpt = null;
			for(String[] strCollTemp:strColl){
				ReportPeriodType rpt = new ReportPeriodType();
				rpt.setGuid(strCollTemp[0]);
				rpt.setCode(strCollTemp[1]);
				rpt.setName(strCollTemp[2]);
				rpt.setParentReportPeriodType(parentRpt);
				rpt.setService(service);
				parentRpt = rpt;
				reportPeriodTypeManager.saveObject(rpt);
			}
			
			ldb.set("success", true);
			ldb.set("reason", "初始化成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
		}

		return AJAX;
	}
	
	@Action(value="updateReportPeriodType")
	public String updateReportPeriodType() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			ReportPeriodType[] ts = (ReportPeriodType[])JSONUtil.stringToArray(json, ReportPeriodType.class);
			for (ReportPeriodType t : ts){
				if(t.getGuid()==null || t.getGuid().trim().length()==0 || t.getGuid().trim().startsWith("NEW")){
					t.setGuid(new GUID().toString());
				}
				t.setService(this.getDefaultService());
				reportPeriodTypeManager.saveObject(t);
			}
			
			ldb.set("success", true);
			ldb.set("reason", "保存成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}

		return AJAX;
	}
	
	@Action(value="index",
			results={@Result(name="success", location="/admin/reportperiod/index.jsp")}
	)
	public String index(){
		return SUCCESS;
	}

	public boolean isHalfyear() {
		return halfyear;
	}

	public void setHalfyear(boolean halfyear) {
		this.halfyear = halfyear;
	}

	public boolean isQurater() {
		return qurater;
	}

	public void setQurater(boolean qurater) {
		this.qurater = qurater;
	}

	public boolean isMonth() {
		return month;
	}

	public void setMonth(boolean month) {
		this.month = month;
	}

	public String getTreeId() {
		return treeId;
	}
	
	public String setTreeId(String treeId) {
		return treeId;
	}
	
	public ReportPeriodType getReportPeriodType() {
		return reportPeriodType;
	}
	public void setReportPeriodType(ReportPeriodType reportPeriodType) {
		this.reportPeriodType = reportPeriodType;
	}
	public ReportPeriodTypeManager getReportPeriodTypeManager() {
		return reportPeriodTypeManager;
	}
	public void setReportPeriodTypeManager(ReportPeriodTypeManager reportPeriodTypeManager) {
		this.reportPeriodTypeManager = reportPeriodTypeManager;
	}
}
